<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>Chapter 15: Object-Oriented Programming | A君的秘密基地</title><meta name="keywords" content="C++ Primer"><meta name="author" content="astralcon"><meta name="copyright" content="astralcon"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="# Exercises Section 15.2.1 Ex15.1一个类的 virtual 成员可以被其派生类重写 Ex15.2被 protected 修饰的类成员可以被该类的派生类访问，而被 private 修饰的类成员则不行。 Ex15.31234567891011121314151617181920class Quote&amp;#123;public:	Quote() &#x3D; default;	Qu">
<meta property="og:type" content="article">
<meta property="og:title" content="Chapter 15: Object-Oriented Programming">
<meta property="og:url" content="https://astralcon.gitee.io/blog/2022/05/14/Chapter-15-Object-Oriented-Programming/index.html">
<meta property="og:site_name" content="A君的秘密基地">
<meta property="og:description" content="# Exercises Section 15.2.1 Ex15.1一个类的 virtual 成员可以被其派生类重写 Ex15.2被 protected 修饰的类成员可以被该类的派生类访问，而被 private 修饰的类成员则不行。 Ex15.31234567891011121314151617181920class Quote&amp;#123;public:	Quote() &#x3D; default;	Qu">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="">
<meta property="article:published_time" content="2022-05-14T06:28:52.000Z">
<meta property="article:modified_time" content="2022-05-14T06:59:43.347Z">
<meta property="article:author" content="astralcon">
<meta property="article:tag" content="C++ Primer">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content=""><link rel="shortcut icon" href="/blog/img/avatar.jpg"><link rel="canonical" href="https://astralcon.gitee.io/blog/2022/05/14/Chapter-15-Object-Oriented-Programming/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/blog/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/blog/',
  algolia: {"appId":"YUB0ZFWBBL","apiKey":"78d0f1fae31a1df3e0cb573725c55558","indexName":"blog","hits":{"per_page":6},"languages":{"input_placeholder":"搜索文章","hits_empty":"找不到您查询的内容：${query}","hits_stats":"找到 ${hits} 条结果，用时 ${time} 毫秒"}},
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'Chapter 15: Object-Oriented Programming',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2022-05-14 14:59:43'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 6.2.0"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/blog/img/avatar.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/blog/archives/"><div class="headline">文章</div><div class="length-num">27</div></a><a href="/blog/tags/"><div class="headline">标签</div><div class="length-num">4</div></a><a href="/blog/categories/"><div class="headline">分类</div><div class="length-num">3</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/blog/"><i class="fa-fw fas fa-home"></i><span> 首頁</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-folder-open"></i><span> 目录</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/blog/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></li><li><a class="site-page child" href="/blog/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/blog/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></li></ul></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-heartbeat"></i><span> 清单</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/blog/game/"><i class="fa-fw fas fa-gamepad"></i><span> 游戏</span></a></li><li><a class="site-page child" href="/blog/book/"><i class="fa-fw fas fa-book"></i><span> 书籍</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/blog/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/blog/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/blog/">A君的秘密基地</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/blog/"><i class="fa-fw fas fa-home"></i><span> 首頁</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-folder-open"></i><span> 目录</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/blog/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></li><li><a class="site-page child" href="/blog/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/blog/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></li></ul></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-heartbeat"></i><span> 清单</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/blog/game/"><i class="fa-fw fas fa-gamepad"></i><span> 游戏</span></a></li><li><a class="site-page child" href="/blog/book/"><i class="fa-fw fas fa-book"></i><span> 书籍</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/blog/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/blog/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">Chapter 15: Object-Oriented Programming</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2022-05-14T06:28:52.000Z" title="发表于 2022-05-14 14:28:52">2022-05-14</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-05-14T06:59:43.347Z" title="更新于 2022-05-14 14:59:43">2022-05-14</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/blog/categories/C/">C++</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-commentcount"><i class="far fa-comments fa-fw post-meta-icon"></i><span class="post-meta-label">评论数:</span><a href="/blog/2022/05/14/Chapter-15-Object-Oriented-Programming/#post-comment" itemprop="discussionUrl"><span class="valine-comment-count" data-xid="/blog/2022/05/14/Chapter-15-Object-Oriented-Programming/" itemprop="commentCount"></span></a></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><p>﻿# Exercises Section 15.2.1</p>
<h2 id="Ex15-1"><a href="#Ex15-1" class="headerlink" title="Ex15.1"></a>Ex15.1</h2><p>一个类的 virtual 成员可以被其派生类重写</p>
<h2 id="Ex15-2"><a href="#Ex15-2" class="headerlink" title="Ex15.2"></a>Ex15.2</h2><p>被 protected 修饰的类成员可以被该类的派生类访问，而被 private 修饰的类成员则不行。</p>
<h2 id="Ex15-3"><a href="#Ex15-3" class="headerlink" title="Ex15.3"></a>Ex15.3</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Quote</span></span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">	<span class="built_in">Quote</span>() = <span class="keyword">default</span>;</span><br><span class="line">	<span class="built_in">Quote</span>(<span class="type">const</span> std::string &amp;book, <span class="type">double</span> sales_price): <span class="built_in">bookNo</span>(book), <span class="built_in">price</span>(sales_price) &#123; &#125;</span><br><span class="line">	<span class="function">std::string <span class="title">isbn</span><span class="params">()</span> <span class="type">const</span> </span>&#123; <span class="keyword">return</span> bookNo; &#125;</span><br><span class="line">	<span class="function"><span class="keyword">virtual</span> <span class="type">double</span> <span class="title">net_price</span><span class="params">(std::<span class="type">size_t</span> n)</span> <span class="type">const</span> </span>&#123; <span class="keyword">return</span> n * price; &#125;</span><br><span class="line">	<span class="keyword">virtual</span> ~<span class="built_in">Quote</span>() = <span class="keyword">default</span>;</span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">	std::string bookNo;</span><br><span class="line"><span class="keyword">protected</span>:</span><br><span class="line">	<span class="type">double</span> price = <span class="number">0.0</span>;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">double</span> <span class="title">print_total</span><span class="params">(ostream &amp;os, <span class="type">const</span> Quote &amp;item, <span class="type">size_t</span> n)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="type">double</span> ret = item.<span class="built_in">net_price</span>(n);</span><br><span class="line">	os &lt;&lt; <span class="string">&quot;ISBN: &quot;</span> &lt;&lt; item.<span class="built_in">isbn</span>() &lt;&lt; <span class="string">&quot; # sold: &quot;</span> &lt;&lt; n &lt;&lt; <span class="string">&quot; total due: &quot;</span> &lt;&lt; ret &lt;&lt; endl;</span><br><span class="line">	<span class="keyword">return</span> ret;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="Exercises-Section-15-2-2"><a href="#Exercises-Section-15-2-2" class="headerlink" title="Exercises Section 15.2.2"></a>Exercises Section 15.2.2</h1><h2 id="Ex15-4"><a href="#Ex15-4" class="headerlink" title="Ex15.4"></a>Ex15.4</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Base</span> &#123; ... &#125;;</span><br><span class="line"></span><br><span class="line">a) <span class="keyword">class</span> <span class="title class_">Derived</span> : <span class="function"><span class="keyword">public</span> <span class="title">Derived</span><span class="params">()</span> </span>&#123; ... &#125;;	<span class="comment">// 错误；一个类不能是自己的派生类</span></span><br><span class="line">b) <span class="keyword">class</span> <span class="title class_">Derived</span> : <span class="keyword">private</span> Base &#123; ... &#125;;	<span class="comment">// 正确</span></span><br><span class="line">c) <span class="keyword">class</span> <span class="title class_">Derived</span> : <span class="keyword">public</span> Base;		<span class="comment">// 错误；声明列表必须完整或者不出现声明列表</span></span><br></pre></td></tr></table></figure>

<h2 id="Ex15-5"><a href="#Ex15-5" class="headerlink" title="Ex15.5"></a>Ex15.5</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Bulk_quote</span> : <span class="keyword">public</span> Quote </span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">	<span class="built_in">Bulk_quote</span>() = <span class="keyword">default</span>;</span><br><span class="line">	<span class="built_in">Bulk_quote</span>(<span class="type">const</span> std::string &amp;book, <span class="type">double</span> p, std::<span class="type">size_t</span> qty, <span class="type">double</span> disc):</span><br><span class="line">			   <span class="built_in">Quote</span>(book, p), <span class="built_in">min_qty</span>(qty), <span class="built_in">discount</span>(disc) &#123; &#125;</span><br><span class="line">	<span class="function"><span class="type">double</span> <span class="title">net_price</span><span class="params">(std::<span class="type">size_t</span>)</span> <span class="type">const</span> <span class="keyword">override</span></span>;</span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">	std::<span class="type">size_t</span> min_qty = <span class="number">0</span>;</span><br><span class="line">	<span class="type">double</span> discount = <span class="number">0.0</span>;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">double</span> <span class="title">Bulk_quote::net_price</span><span class="params">(<span class="type">size_t</span> cnt)</span> <span class="type">const</span> </span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">if</span> (cnt &gt;= min_qty)</span><br><span class="line">		<span class="keyword">return</span> cnt * (<span class="number">1</span> - discount) * price;</span><br><span class="line">	<span class="keyword">else</span> </span><br><span class="line">		<span class="keyword">return</span> cnt * price;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="Ex15-6"><a href="#Ex15-6" class="headerlink" title="Ex15.6"></a>Ex15.6</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Quote</span></span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">	<span class="built_in">Quote</span>() = <span class="keyword">default</span>;</span><br><span class="line">	<span class="built_in">Quote</span>(<span class="type">const</span> std::string &amp;book, <span class="type">double</span> sales_price): <span class="built_in">bookNo</span>(book), <span class="built_in">price</span>(sales_price) &#123; &#125;</span><br><span class="line">	<span class="function">std::string <span class="title">isbn</span><span class="params">()</span> <span class="type">const</span> </span>&#123; <span class="keyword">return</span> bookNo; &#125;</span><br><span class="line">	<span class="function"><span class="keyword">virtual</span> <span class="type">double</span> <span class="title">net_price</span><span class="params">(std::<span class="type">size_t</span> n)</span> <span class="type">const</span> </span>&#123; <span class="keyword">return</span> n * price; &#125;</span><br><span class="line">	<span class="keyword">virtual</span> ~<span class="built_in">Quote</span>() = <span class="keyword">default</span>;</span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">	std::string bookNo;</span><br><span class="line"><span class="keyword">protected</span>:</span><br><span class="line">	<span class="type">double</span> price = <span class="number">0.0</span>;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Bulk_quote</span> : <span class="keyword">public</span> Quote </span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">	<span class="built_in">Bulk_quote</span>() = <span class="keyword">default</span>;</span><br><span class="line">	<span class="built_in">Bulk_quote</span>(<span class="type">const</span> std::string &amp;book, <span class="type">double</span> p, std::<span class="type">size_t</span> qty, <span class="type">double</span> disc):</span><br><span class="line">			   <span class="built_in">Quote</span>(book, p), <span class="built_in">min_qty</span>(qty), <span class="built_in">discount</span>(disc) &#123; &#125;</span><br><span class="line">	<span class="function"><span class="type">double</span> <span class="title">net_price</span><span class="params">(std::<span class="type">size_t</span>)</span> <span class="type">const</span> <span class="keyword">override</span></span>;</span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">	std::<span class="type">size_t</span> min_qty = <span class="number">0</span>;</span><br><span class="line">	<span class="type">double</span> discount = <span class="number">0.0</span>;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">double</span> <span class="title">print_total</span><span class="params">(ostream &amp;os, <span class="type">const</span> Quote &amp;item, <span class="type">size_t</span> n)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="type">double</span> ret = item.<span class="built_in">net_price</span>(n);</span><br><span class="line">	os &lt;&lt; <span class="string">&quot;ISBN: &quot;</span> &lt;&lt; item.<span class="built_in">isbn</span>() &lt;&lt; <span class="string">&quot; # sold: &quot;</span> &lt;&lt; n &lt;&lt; <span class="string">&quot; total due: &quot;</span> &lt;&lt; ret &lt;&lt; endl;</span><br><span class="line">	<span class="keyword">return</span> ret;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">double</span> <span class="title">Bulk_quote::net_price</span><span class="params">(<span class="type">size_t</span> cnt)</span> <span class="type">const</span> </span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">if</span> (cnt &gt;= min_qty)</span><br><span class="line">		<span class="keyword">return</span> cnt * (<span class="number">1</span> - discount) * price;</span><br><span class="line">	<span class="keyword">else</span> </span><br><span class="line">		<span class="keyword">return</span> cnt * price;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="function">Quote <span class="title">q</span><span class="params">(<span class="string">&quot;199-9-X&quot;</span>, <span class="number">20</span>)</span></span>;</span><br><span class="line">	<span class="function">Bulk_quote <span class="title">bq</span><span class="params">(<span class="string">&quot;199-9-X&quot;</span>, <span class="number">20</span>, <span class="number">10</span>, <span class="number">0.2</span>)</span></span>;</span><br><span class="line">	<span class="built_in">print_total</span>(cout, q, <span class="number">10</span>);</span><br><span class="line">	<span class="built_in">print_total</span>(cout, bq, <span class="number">10</span>);</span><br><span class="line">	<span class="built_in">system</span>(<span class="string">&quot;pause&quot;</span>);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="Ex15-7"><a href="#Ex15-7" class="headerlink" title="Ex15.7"></a>Ex15.7</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Limit_Quote</span> : <span class="keyword">public</span> Quote</span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">	<span class="built_in">Limit_Quote</span>() = <span class="keyword">default</span>;</span><br><span class="line">	<span class="built_in">Limit_Quote</span>(<span class="type">const</span> std::string &amp;book, <span class="type">double</span> p, std::<span class="type">size_t</span> cnt, <span class="type">double</span> disc):</span><br><span class="line">			 	<span class="built_in">Quote</span>(book, p), <span class="built_in">count</span>(cnt), <span class="built_in">discount</span>(disc) &#123; &#125;</span><br><span class="line">	<span class="function"><span class="type">double</span> <span class="title">net_price</span><span class="params">(std::<span class="type">size_t</span> sz)</span> <span class="type">const</span> <span class="keyword">override</span></span>; </span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">	std::<span class="type">size_t</span> count = <span class="number">0</span>;</span><br><span class="line">	<span class="type">double</span> discount = <span class="number">0</span>;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">double</span> <span class="title">Limit_Quote::net_price</span><span class="params">(<span class="type">size_t</span> sz)</span> <span class="type">const</span> </span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">if</span> (count &gt;= sz)</span><br><span class="line">		<span class="keyword">return</span> sz * (<span class="number">1</span> - discount) * price;</span><br><span class="line">	<span class="keyword">else</span></span><br><span class="line">		<span class="keyword">return</span> sz * price;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="Exercises-Section-15-2-3"><a href="#Exercises-Section-15-2-3" class="headerlink" title="Exercises Section 15.2.3"></a>Exercises Section 15.2.3</h1><h2 id="Ex15-8"><a href="#Ex15-8" class="headerlink" title="Ex15.8"></a>Ex15.8</h2><p>静态类型：在编译时刻就知道的类型；变量声明类型或者表达式产生的类型。<br>动态类型：只在运行时刻才知道的类型；内存中变量或者表达式表示的对象类型。</p>
<h2 id="Ex15-9"><a href="#Ex15-9" class="headerlink" title="Ex15.9"></a>Ex15.9</h2><ol>
<li>当对象的静态类型和对象的动态类型之间存在隐式转换</li>
<li>当基类指针或引用指向派生类对象</li>
<li>当虚函数通过指针或引用被调用</li>
</ol>
<h2 id="Ex15-10"><a href="#Ex15-10" class="headerlink" title="Ex15.10"></a>Ex15.10</h2><p>ifstream 类传递了一个派生类对象给 read 函数。</p>
<h1 id="Exercises-Section-15-3"><a href="#Exercises-Section-15-3" class="headerlink" title="Exercises Section 15.3"></a>Exercises Section 15.3</h1><h2 id="Ex15-11"><a href="#Ex15-11" class="headerlink" title="Ex15.11"></a>Ex15.11</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Quote</span></span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">	<span class="built_in">Quote</span>() = <span class="keyword">default</span>;</span><br><span class="line">	<span class="built_in">Quote</span>(<span class="type">const</span> std::string &amp;book, <span class="type">double</span> sales_price): <span class="built_in">bookNo</span>(book), <span class="built_in">price</span>(sales_price) &#123; &#125;</span><br><span class="line">	<span class="function">std::string <span class="title">isbn</span><span class="params">()</span> <span class="type">const</span> </span>&#123; <span class="keyword">return</span> bookNo; &#125;</span><br><span class="line">	<span class="function"><span class="keyword">virtual</span> <span class="type">double</span> <span class="title">net_price</span><span class="params">(std::<span class="type">size_t</span> n)</span> <span class="type">const</span> </span>&#123; <span class="keyword">return</span> n * price; &#125;</span><br><span class="line">	<span class="function"><span class="keyword">virtual</span> <span class="type">void</span> <span class="title">debug</span><span class="params">(ostream &amp;os)</span> </span>&#123; os &lt;&lt; <span class="string">&quot;BookNO: &quot;</span> &lt;&lt; bookNo &lt;&lt; <span class="string">&quot; price: &quot;</span> &lt;&lt; price &lt;&lt; endl; &#125;</span><br><span class="line">	<span class="keyword">virtual</span> ~<span class="built_in">Quote</span>() = <span class="keyword">default</span>;</span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">	std::string bookNo;</span><br><span class="line"><span class="keyword">protected</span>:</span><br><span class="line">	<span class="type">double</span> price = <span class="number">0.0</span>;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h2 id="Ex15-12"><a href="#Ex15-12" class="headerlink" title="Ex15.12"></a>Ex15.12</h2><p> override：如果在派生类中用 override 标记了某函数，而基类中没有对应的该函数，则发生错误。</p>
<p>final：基类中某函数定义为 final，那么派生类中对其重写则发生错误。</p>
<h2 id="Ex15-13"><a href="#Ex15-13" class="headerlink" title="Ex15.13"></a>Ex15.13</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">base</span></span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">	<span class="function">string <span class="title">name</span><span class="params">()</span> </span>&#123; <span class="keyword">return</span> basename; &#125;</span><br><span class="line">	<span class="function"><span class="keyword">virtual</span> <span class="type">void</span> <span class="title">print</span><span class="params">(ostream &amp;os)</span> </span>&#123; os &lt;&lt; basename; &#125;	</span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">	string basename;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">derived</span> : <span class="keyword">public</span> base</span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">	<span class="comment">// 如果派生虚函数想要调用基类版本但是省略了范围操作符，则会调用派生版本自身，产生无限递归</span></span><br><span class="line">	<span class="function"><span class="type">void</span> <span class="title">print</span><span class="params">(ostream &amp;os)</span> </span>&#123; <span class="built_in">print</span>(os); os &lt;&lt; <span class="string">&quot; &quot;</span> &lt;&lt; i; &#125;	</span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">	<span class="type">int</span> i;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>修改后如下：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">base</span></span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">	<span class="function">string <span class="title">name</span><span class="params">()</span> </span>&#123; <span class="keyword">return</span> basename; &#125;</span><br><span class="line">	<span class="function"><span class="keyword">virtual</span> <span class="type">void</span> <span class="title">print</span><span class="params">(ostream &amp;os)</span> </span>&#123; os &lt;&lt; basename; &#125;</span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">	string basename;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">derived</span> : <span class="keyword">public</span> base</span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">	<span class="function"><span class="type">void</span> <span class="title">print</span><span class="params">(ostream &amp;os)</span> </span>&#123; base::<span class="built_in">print</span>(os); os &lt;&lt; <span class="string">&quot; &quot;</span> &lt;&lt; i; &#125;</span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">	<span class="type">int</span> i;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h2 id="Ex15-14"><a href="#Ex15-14" class="headerlink" title="Ex15.14"></a>Ex15.14</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">base bobj;		base *bp1 = &amp;bobj;	base &amp;br1 = bobj;</span><br><span class="line">derived dobj;	base *bp2 = &amp;dobj;	base &amp;br2 = dobj;</span><br><span class="line">a) bobj.<span class="built_in">print</span>();	<span class="comment">// 在编译时刻被调用</span></span><br><span class="line">b) dobj.<span class="built_in">print</span>();	<span class="comment">// 在运行时刻被调用</span></span><br><span class="line">c) bp1-&gt;<span class="built_in">name</span>();		<span class="comment">// 在编译时刻被调用</span></span><br><span class="line">d) bp2-&gt;<span class="built_in">name</span>();		<span class="comment">// 在编译时刻被调用</span></span><br><span class="line">e) br1.<span class="built_in">print</span>();		<span class="comment">// 在编译时刻被调用</span></span><br><span class="line">f) br2.<span class="built_in">print</span>();		<span class="comment">// 在运行时刻被调用</span></span><br></pre></td></tr></table></figure>

<h1 id="Exercises-Section-15-4"><a href="#Exercises-Section-15-4" class="headerlink" title="Exercises Section 15.4"></a>Exercises Section 15.4</h1><h2 id="Ex15-15"><a href="#Ex15-15" class="headerlink" title="Ex15.15"></a>Ex15.15</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Disc_quote</span> : <span class="keyword">public</span> Quote</span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">	<span class="built_in">Disc_quote</span>() = <span class="keyword">default</span>;</span><br><span class="line">	<span class="built_in">Disc_quote</span>(<span class="type">const</span> std::string &amp;book, <span class="type">double</span> price, std::<span class="type">size_t</span> qty, <span class="type">double</span> disc):</span><br><span class="line">			   <span class="built_in">Quote</span>(book, price), <span class="built_in">quantity</span>(qty), <span class="built_in">discount</span>(disc) &#123; &#125;</span><br><span class="line">	<span class="function"><span class="type">double</span> <span class="title">net_price</span><span class="params">(std::<span class="type">size_t</span>)</span> <span class="type">const</span> </span>= <span class="number">0</span>; </span><br><span class="line"><span class="keyword">protected</span>:</span><br><span class="line">	std::<span class="type">size_t</span> quantity = <span class="number">0</span>;</span><br><span class="line">	<span class="type">double</span> discount = <span class="number">0.0</span>;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Bulk_quote</span> : <span class="keyword">public</span> Disc_quote</span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">	<span class="built_in">Bulk_quote</span>() = <span class="keyword">default</span>;</span><br><span class="line">	<span class="built_in">Bulk_quote</span>(<span class="type">const</span> std::string &amp;book, <span class="type">double</span> price, std::<span class="type">size_t</span> qty, <span class="type">double</span> disc):</span><br><span class="line">				<span class="built_in">Disc_quote</span>(book, price, qty, disc) &#123; &#125;</span><br><span class="line">	<span class="function"><span class="type">double</span> <span class="title">net_price</span><span class="params">(std::<span class="type">size_t</span>)</span> <span class="type">const</span> <span class="keyword">override</span></span>;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h2 id="Ex15-16"><a href="#Ex15-16" class="headerlink" title="Ex15.16"></a>Ex15.16</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Limit_quote</span> : <span class="keyword">public</span> Disc_quote</span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">	<span class="built_in">Limit_quote</span>() = <span class="keyword">default</span>;</span><br><span class="line">	<span class="built_in">Limit_quote</span>(<span class="type">const</span> std::string &amp;book, <span class="type">double</span> price, std::<span class="type">size_t</span> qty, <span class="type">double</span> disc):</span><br><span class="line">				<span class="built_in">Disc_quote</span>(book, price, qty, disc) &#123; &#125;</span><br><span class="line">	<span class="function"><span class="type">double</span> <span class="title">net_price</span><span class="params">(std::<span class="type">size_t</span>)</span> <span class="type">const</span> <span class="keyword">override</span></span>;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">double</span> <span class="title">Limit_quote::net_price</span><span class="params">(<span class="type">size_t</span> sz)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">if</span> (quantity &gt;= sz)</span><br><span class="line">		<span class="keyword">return</span> sz * (<span class="number">1</span> - discount) * price;</span><br><span class="line">	<span class="keyword">else</span> </span><br><span class="line">		<span class="keyword">return</span> sz * price;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="Ex15-17"><a href="#Ex15-17" class="headerlink" title="Ex15.17"></a>Ex15.17</h2><p><img src="https://img-blog.csdnimg.cn/20210615141831509.png#pic_center" alt="在这里插入图片描述"></p>
<h1 id="Exercises-Section-15-5"><a href="#Exercises-Section-15-5" class="headerlink" title="Exercises Section 15.5"></a>Exercises Section 15.5</h1><h2 id="Ex15-18"><a href="#Ex15-18" class="headerlink" title="Ex15.18"></a>Ex15.18</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">Base *p = &amp;d1;	<span class="comment">// d1 has type Pub_Derv</span></span><br><span class="line">p = &amp;d2;		<span class="comment">// d2 has type Priv_Derv</span></span><br><span class="line">p = &amp;d3;		<span class="comment">// d3 has type Prot_Derv</span></span><br><span class="line">p = &amp;dd1;		<span class="comment">// dd1 has type Derived_from_Public</span></span><br><span class="line">p = &amp;dd2;		<span class="comment">// dd2 has type Derived_from_Private</span></span><br><span class="line">p = &amp;dd3;		<span class="comment">// dd3 has type Derived_from_Protected</span></span><br></pre></td></tr></table></figure>

<p>d1 和 dd1 能够合法赋值，只有派生类继承基类为 public 时，用户才能进行派生类到基类的转换。</p>
<h2 id="Ex15-19"><a href="#Ex15-19" class="headerlink" title="Ex15.19"></a>Ex15.19</h2><p> Derived_from_Private 不合法；<br> 当D继承自B：对于用户代码，只有 public 继承，能使用D到B的转换；<br> 对于D的成员和友元，任何继承方式都能使用D到B的转换；<br> 对于D的派生类的成员和友元，public 和 protected 继承能使用D到B的转换。</p>
<h2 id="Ex15-20"><a href="#Ex15-20" class="headerlink" title="Ex15.20"></a>Ex15.20</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 注释掉的代码非法</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"> </span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"> </span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Base</span> </span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">	<span class="function"><span class="type">void</span> <span class="title">pub_mem</span><span class="params">()</span></span>;</span><br><span class="line"><span class="keyword">protected</span>:</span><br><span class="line">	<span class="type">int</span> prot_mem;</span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">	<span class="type">char</span> priv_mem;</span><br><span class="line">&#125;;</span><br><span class="line"> </span><br><span class="line"><span class="keyword">struct</span> <span class="title class_">Pub_Derv</span>: <span class="keyword">public</span> Base </span><br><span class="line">&#123;</span><br><span class="line">	<span class="function"><span class="type">int</span> <span class="title">f</span><span class="params">()</span> </span>&#123; <span class="keyword">return</span> prot_mem; &#125;</span><br><span class="line">	<span class="function"><span class="type">void</span> <span class="title">memfcn</span><span class="params">(Base &amp;b)</span></span></span><br><span class="line"><span class="function">	</span>&#123;</span><br><span class="line">		b = *<span class="keyword">this</span>;</span><br><span class="line">		cout &lt;&lt; <span class="string">&quot;Pub_Derv&quot;</span> &lt;&lt; endl;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;;</span><br><span class="line"> </span><br><span class="line"><span class="keyword">struct</span> <span class="title class_">Priv_Derv</span>: <span class="keyword">private</span> Base </span><br><span class="line">&#123;</span><br><span class="line">	<span class="function"><span class="type">int</span> <span class="title">f1</span><span class="params">()</span> <span class="type">const</span> </span>&#123; <span class="keyword">return</span> prot_mem; &#125;</span><br><span class="line">	<span class="function"><span class="type">void</span> <span class="title">memfcn</span><span class="params">(Base &amp;b)</span></span></span><br><span class="line"><span class="function">	</span>&#123;</span><br><span class="line">		b = *<span class="keyword">this</span>;</span><br><span class="line">		cout &lt;&lt; <span class="string">&quot;Priv_Derv&quot;</span> &lt;&lt; endl;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;;</span><br><span class="line"> </span><br><span class="line"><span class="keyword">struct</span> <span class="title class_">Prot_Derv</span>: <span class="keyword">protected</span> Base </span><br><span class="line">&#123;</span><br><span class="line">	<span class="function"><span class="type">int</span> <span class="title">f2</span><span class="params">()</span> </span>&#123; <span class="keyword">return</span> prot_mem; &#125;</span><br><span class="line">	<span class="function"><span class="type">void</span> <span class="title">memfcn</span><span class="params">(Base &amp;b)</span></span></span><br><span class="line"><span class="function">	</span>&#123;</span><br><span class="line">		b = *<span class="keyword">this</span>;</span><br><span class="line">		cout &lt;&lt; <span class="string">&quot;Prot_Derv&quot;</span> &lt;&lt; endl;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;;</span><br><span class="line"> </span><br><span class="line"><span class="keyword">struct</span> <span class="title class_">Derived_from_Public</span>: <span class="keyword">public</span> Pub_Derv </span><br><span class="line">&#123;</span><br><span class="line">	<span class="function"><span class="type">int</span> <span class="title">use_base</span><span class="params">()</span> </span>&#123; <span class="keyword">return</span> prot_mem; &#125;</span><br><span class="line">	<span class="function"><span class="type">void</span> <span class="title">memfcn</span><span class="params">(Base &amp;b)</span></span></span><br><span class="line"><span class="function">	</span>&#123;</span><br><span class="line">		b = *<span class="keyword">this</span>;</span><br><span class="line">		cout &lt;&lt; <span class="string">&quot;Derived_from_Public&quot;</span> &lt;&lt; endl;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">struct Derived_from_Private: public Priv_Derv </span></span><br><span class="line"><span class="comment">&#123;</span></span><br><span class="line"><span class="comment">	int use_base() &#123; return prot_mem; &#125;</span></span><br><span class="line"><span class="comment">	void memfcn(Base &amp;b)</span></span><br><span class="line"><span class="comment">	&#123;</span></span><br><span class="line"><span class="comment">		b = *this;</span></span><br><span class="line"><span class="comment">		cout &lt;&lt; &quot;Derived_from_Private&quot; &lt;&lt; endl;</span></span><br><span class="line"><span class="comment">	&#125;</span></span><br><span class="line"><span class="comment">&#125;;</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="keyword">struct</span> <span class="title class_">Derived_from_Protected</span>: <span class="keyword">protected</span> Prot_Derv </span><br><span class="line">&#123;</span><br><span class="line">	<span class="function"><span class="type">int</span> <span class="title">use_base</span><span class="params">()</span> </span>&#123; <span class="keyword">return</span> prot_mem; &#125;</span><br><span class="line">	<span class="function"><span class="type">void</span> <span class="title">memfcn</span><span class="params">(Base &amp;b)</span></span></span><br><span class="line"><span class="function">	</span>&#123;</span><br><span class="line">		b = *<span class="keyword">this</span>;</span><br><span class="line">		cout &lt;&lt; <span class="string">&quot;Derived_from_Protected&quot;</span> &lt;&lt; endl;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;;</span><br><span class="line"> </span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	Pub_Derv d1;</span><br><span class="line">	Priv_Derv d2;</span><br><span class="line">	Prot_Derv d3;</span><br><span class="line">	</span><br><span class="line">	Derived_from_Public dd1;</span><br><span class="line"><span class="comment">//	Derived_from_Private dd2;</span></span><br><span class="line">	Derived_from_Protected dd3;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">//用户代码</span></span><br><span class="line">	Base base;</span><br><span class="line">	Base *p = &amp;d1;</span><br><span class="line"><span class="comment">//	p = &amp;d2;</span></span><br><span class="line"><span class="comment">//	p = &amp;d3;</span></span><br><span class="line">	</span><br><span class="line">	p = &amp;dd1;</span><br><span class="line"><span class="comment">//	p = &amp;dd2;</span></span><br><span class="line"><span class="comment">//	p = &amp;dd3;</span></span><br><span class="line">	</span><br><span class="line">	<span class="comment">//成员调用</span></span><br><span class="line">	d1.<span class="built_in">memfcn</span>(base);</span><br><span class="line">	d2.<span class="built_in">memfcn</span>(base);</span><br><span class="line">	d3.<span class="built_in">memfcn</span>(base);</span><br><span class="line">	</span><br><span class="line">	<span class="comment">//派生类的成员调用</span></span><br><span class="line">	dd1.<span class="built_in">memfcn</span>(base);</span><br><span class="line"><span class="comment">//	dd2.memfcn(base);</span></span><br><span class="line">	dd3.<span class="built_in">memfcn</span>(base);</span><br><span class="line">	</span><br><span class="line">	<span class="built_in">system</span>(<span class="string">&quot;pause&quot;</span>);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="Ex-15-21"><a href="#Ex-15-21" class="headerlink" title="Ex 15.21"></a>Ex 15.21</h2><p>(b)的继承体系：公共基类 Figure，派生类 Figure_2D 和 Figure_3D 继承自 Figure，Retangle 和 Circle 继承自 Figure_2D，Sphere 和 Cone 继承自Figure_3D。</p>
<h2 id="Ex15-22"><a href="#Ex15-22" class="headerlink" title="Ex15.22"></a>Ex15.22</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Figure</span></span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">	<span class="built_in">Figure</span>() = <span class="keyword">default</span>;</span><br><span class="line">	<span class="built_in">Figure</span>(<span class="type">double</span>, <span class="type">double</span>);</span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">	<span class="type">double</span> x, y;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Figure_2D</span> : <span class="keyword">public</span> Figure</span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">	<span class="built_in">Figure_2D</span>() = <span class="keyword">default</span>;</span><br><span class="line">	<span class="built_in">Figure_2D</span>(<span class="type">double</span>, <span class="type">double</span>);</span><br><span class="line">	<span class="function"><span class="keyword">virtual</span> <span class="type">double</span> <span class="title">area</span><span class="params">()</span> </span>= <span class="number">0</span>;</span><br><span class="line">	<span class="function"><span class="keyword">virtual</span> <span class="type">double</span> <span class="title">perimeter</span><span class="params">()</span> </span>= <span class="number">0</span>;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Figure_3D</span> : <span class="keyword">public</span> Figure</span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">	<span class="built_in">Figure_3D</span>() = <span class="keyword">default</span>;</span><br><span class="line">	<span class="built_in">Figure_3D</span>(<span class="type">double</span>, <span class="type">double</span>, <span class="type">double</span>);</span><br><span class="line">	<span class="function"><span class="keyword">virtual</span> <span class="type">double</span> <span class="title">cubage</span><span class="params">()</span> </span>= <span class="number">0</span>;</span><br><span class="line"><span class="keyword">protected</span>:</span><br><span class="line">	<span class="type">double</span> z;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Retangle</span> : <span class="keyword">public</span> Figure_2D</span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">	<span class="built_in">Retangle</span>() = <span class="keyword">default</span>;</span><br><span class="line">	<span class="built_in">Retangle</span>(<span class="type">double</span>, <span class="type">double</span>);</span><br><span class="line">	<span class="function"><span class="type">double</span> <span class="title">area</span><span class="params">()</span> <span class="keyword">override</span></span>;</span><br><span class="line">	<span class="function"><span class="type">double</span> <span class="title">perimeter</span><span class="params">()</span> <span class="keyword">override</span></span>;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Circle</span> : <span class="keyword">public</span> Figure_2D</span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">	<span class="built_in">Circle</span>() = <span class="keyword">default</span>;</span><br><span class="line">	<span class="built_in">Circle</span>(<span class="type">double</span>, <span class="type">double</span>);</span><br><span class="line">	<span class="function"><span class="type">double</span> <span class="title">area</span><span class="params">()</span> <span class="keyword">override</span></span>;</span><br><span class="line">	<span class="function"><span class="type">double</span> <span class="title">perimeter</span><span class="params">()</span> <span class="keyword">override</span></span>;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Sphere</span> : <span class="keyword">public</span> Figure_3D</span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">	<span class="built_in">Sphere</span>() = <span class="keyword">default</span>;</span><br><span class="line">	<span class="built_in">Sphere</span>(<span class="type">double</span>, <span class="type">double</span>, <span class="type">double</span>);</span><br><span class="line">	<span class="function"><span class="type">double</span> <span class="title">cubage</span><span class="params">()</span> <span class="keyword">override</span></span>;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Cone</span> : <span class="keyword">public</span> Figure_3D</span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">	<span class="built_in">Cone</span>() = <span class="keyword">default</span>;</span><br><span class="line">	<span class="built_in">Cone</span>(<span class="type">double</span>, <span class="type">double</span>, <span class="type">double</span>);</span><br><span class="line">	<span class="function"><span class="type">double</span> <span class="title">cubage</span><span class="params">()</span> <span class="keyword">override</span></span>;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h1 id="Exercises-Section-15-6"><a href="#Exercises-Section-15-6" class="headerlink" title="Exercises Section 15.6"></a>Exercises Section 15.6</h1><h2 id="Ex15-23"><a href="#Ex15-23" class="headerlink" title="Ex15.23"></a>Ex15.23</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Base</span></span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">	<span class="function"><span class="keyword">virtual</span> <span class="type">int</span> <span class="title">fcn</span><span class="params">()</span></span>;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">D1</span> : <span class="keyword">public</span> Base</span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">	<span class="function"><span class="type">int</span> <span class="title">fcn</span><span class="params">()</span></span>;	</span><br><span class="line">	<span class="function"><span class="keyword">virtual</span> <span class="type">void</span> <span class="title">f2</span><span class="params">()</span></span>;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">D2</span> : <span class="keyword">public</span> D1 </span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">	<span class="function"><span class="type">int</span> <span class="title">fcn</span><span class="params">(<span class="type">int</span>)</span></span>;</span><br><span class="line">	<span class="function"><span class="type">int</span> <span class="title">fcn</span><span class="params">()</span></span>;</span><br><span class="line">	<span class="function"><span class="type">void</span> <span class="title">f2</span><span class="params">()</span></span>;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>调用语句解析：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">Base bobj; D1 d1obj; D2 d2obj;</span><br><span class="line">Base *bp1 = &amp;bobj, *bp2 = &amp;d1obj, *bp3 = d2obj;</span><br><span class="line">bp1-&gt;<span class="built_in">fcn</span>();		<span class="comment">// 调用Base::fcn</span></span><br><span class="line">bp2-&gt;<span class="built_in">fcn</span>();		<span class="comment">// 调用D1::fcn</span></span><br><span class="line">bp3-&gt;<span class="built_in">fcn</span>();		<span class="comment">// 调用D2::fcn</span></span><br><span class="line"></span><br><span class="line">D1 *d1p = &amp;d1obj; D2 *d2p = &amp;d2obj;</span><br><span class="line">bp2-&gt;<span class="built_in">f2</span>();		<span class="comment">// 错误，Base没有f2()</span></span><br><span class="line">d1p-&gt;<span class="built_in">f2</span>();		<span class="comment">// 调用D1::f2()</span></span><br><span class="line">d2p-&gt;<span class="built_in">f2</span>();		<span class="comment">// 调用D2::f2()</span></span><br><span class="line"></span><br><span class="line">Base *p1 = &amp;d2obj; D1 *p2 = &amp;d2obj; D2 *p3 = &amp;d2obj;</span><br><span class="line">p1-&gt;<span class="built_in">fcn</span>(<span class="number">42</span>);	<span class="comment">// 错误，Base没有fcn(int)</span></span><br><span class="line">p2-&gt;<span class="built_in">fcn</span>(<span class="number">42</span>);	<span class="comment">// 错误，D1没有fcn(int)</span></span><br><span class="line">p3-&gt;<span class="built_in">fcn</span>(<span class="number">42</span>);	<span class="comment">// 静态绑定，调用D2::fcn(int)</span></span><br></pre></td></tr></table></figure>

<h1 id="Exercises-Section-15-7-1"><a href="#Exercises-Section-15-7-1" class="headerlink" title="Exercises Section 15.7.1"></a>Exercises Section 15.7.1</h1><h2 id="Ex15-24"><a href="#Ex15-24" class="headerlink" title="Ex15.24"></a>Ex15.24</h2><p>基类需要被 virtual 修饰的析构函数，在静态类型和动态类型不同时，保证运行正确的析构函数。</p>
<p>虚析构函数必须清除该类中的成员数据，可以定义为 &#x3D;  default 版本。</p>
<h1 id="Exercises-Section-15-7-2"><a href="#Exercises-Section-15-7-2" class="headerlink" title="Exercises Section 15.7.2"></a>Exercises Section 15.7.2</h1><h2 id="Ex15-25"><a href="#Ex15-25" class="headerlink" title="Ex15.25"></a>Ex15.25</h2><p>Disc_quote 的默认构造函数会调用 Quote 的默认构造函数，完成基类部分的成员初始化工作；如果去掉该默认构造函数，Bulk_quote 的默认构造函数将无法完成其基类部分的初始化工作。</p>
<h1 id="Exercises-Section-15-7-3"><a href="#Exercises-Section-15-7-3" class="headerlink" title="Exercises Section 15.7.3"></a>Exercises Section 15.7.3</h1><h2 id="Ex15-26"><a href="#Ex15-26" class="headerlink" title="Ex15.26"></a>Ex15.26</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Quote</span></span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">	<span class="built_in">Quote</span>() = <span class="keyword">default</span>;</span><br><span class="line">	<span class="built_in">Quote</span>(<span class="type">const</span> string &amp;book, <span class="type">double</span> sales_price): <span class="built_in">bookNo</span>(book), <span class="built_in">price</span>(sales_price) &#123; cout &lt;&lt; <span class="string">&quot;Quote constructor&quot;</span> &lt;&lt; endl; &#125;</span><br><span class="line">	<span class="built_in">Quote</span>(Quote &amp;q): <span class="built_in">bookNo</span>(q.bookNo), <span class="built_in">price</span>(q.price) &#123; cout &lt;&lt; <span class="string">&quot;Quote copy constructor&quot;</span> &lt;&lt; endl; &#125;</span><br><span class="line">	<span class="built_in">Quote</span>(Quote &amp;&amp;q):<span class="built_in">bookNo</span>(std::<span class="built_in">move</span>(q.bookNo)), <span class="built_in">price</span>(std::<span class="built_in">move</span>(q.price)) &#123; cout &lt;&lt; <span class="string">&quot;Quote move constructor&quot;</span> &lt;&lt; endl; &#125;</span><br><span class="line">	Quote &amp; <span class="keyword">operator</span>=(<span class="type">const</span> Quote &amp;q)</span><br><span class="line">	&#123;</span><br><span class="line">		bookNo = q.bookNo;</span><br><span class="line">		price = q.price;</span><br><span class="line">		cout &lt;&lt; <span class="string">&quot;Quote operator=&quot;</span> &lt;&lt; endl;</span><br><span class="line">		<span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	Quote &amp; <span class="keyword">operator</span>=(Quote &amp;&amp;q)</span><br><span class="line">	&#123;</span><br><span class="line">		bookNo = std::<span class="built_in">move</span>(q.bookNo);</span><br><span class="line">		price = std::<span class="built_in">move</span>(q.price);</span><br><span class="line">		cout &lt;&lt; <span class="string">&quot;Quote operator move = &quot;</span> &lt;&lt; endl;</span><br><span class="line">		<span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function">string <span class="title">isbn</span><span class="params">()</span> <span class="type">const</span> </span>&#123; <span class="keyword">return</span> bookNo; &#125;</span><br><span class="line">	<span class="function"><span class="keyword">virtual</span> <span class="type">double</span> <span class="title">net_price</span><span class="params">(<span class="type">size_t</span> n)</span> <span class="type">const</span> </span></span><br><span class="line"><span class="function">	</span>&#123;</span><br><span class="line">		<span class="keyword">return</span> n * price;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">virtual</span> <span class="type">void</span> <span class="title">debug</span><span class="params">()</span> </span>&#123; cout &lt;&lt; <span class="string">&quot;bookNo: &quot;</span> &lt;&lt; bookNo &lt;&lt; <span class="string">&quot; price: &quot;</span> &lt;&lt; price &lt;&lt; endl; &#125;</span><br><span class="line">	<span class="keyword">virtual</span> ~<span class="built_in">Quote</span>() &#123; cout &lt;&lt; <span class="string">&quot;Quote destructor&quot;</span> &lt;&lt; endl; &#125;</span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">	string bookNo;</span><br><span class="line"><span class="keyword">protected</span>:</span><br><span class="line">	<span class="type">double</span> price = <span class="number">0.0</span>;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Bulk_quote</span> : <span class="keyword">public</span> Quote</span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">	<span class="built_in">Bulk_quote</span>() = <span class="keyword">default</span>;</span><br><span class="line">	<span class="built_in">Bulk_quote</span>(<span class="type">const</span> string &amp;book, <span class="type">double</span> p, <span class="type">size_t</span> qty, <span class="type">double</span> disc):</span><br><span class="line">				<span class="built_in">Quote</span>(book, p), <span class="built_in">min_qty</span>(qty), <span class="built_in">discount</span>(disc) &#123; cout &lt;&lt; <span class="string">&quot;Bulk_quote constructor&quot;</span> &lt;&lt; endl; &#125;</span><br><span class="line">	<span class="built_in">Bulk_quote</span>(Bulk_quote &amp;bq): <span class="built_in">Quote</span>(bq), <span class="built_in">min_qty</span>(bq.min_qty), <span class="built_in">discount</span>(bq.discount) &#123; cout &lt;&lt; <span class="string">&quot;Bulk_quote copy constructor&quot;</span> &lt;&lt; endl; &#125;</span><br><span class="line">	<span class="built_in">Bulk_quote</span>(Bulk_quote &amp;&amp;bq): <span class="built_in">Quote</span>(std::<span class="built_in">move</span>(bq)), <span class="built_in">min_qty</span>(std::<span class="built_in">move</span>(bq.min_qty)), <span class="built_in">discount</span>(std::<span class="built_in">move</span>(bq.discount)) </span><br><span class="line">	&#123; cout &lt;&lt; <span class="string">&quot;Bulk_quote move constructor&quot;</span> &lt;&lt; endl; &#125;</span><br><span class="line">	Bulk_quote &amp; <span class="keyword">operator</span>=(<span class="type">const</span> Bulk_quote &amp;bq)</span><br><span class="line">	&#123;</span><br><span class="line">		Quote::<span class="keyword">operator</span>=(bq);</span><br><span class="line">		min_qty = bq.min_qty;</span><br><span class="line">		discount = bq.discount;</span><br><span class="line">		cout &lt;&lt; <span class="string">&quot;Bulk_quote operator= constructor&quot;</span> &lt;&lt; endl;</span><br><span class="line">		<span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	Bulk_quote &amp; <span class="keyword">operator</span>=(<span class="type">const</span> Bulk_quote &amp;&amp;bq)</span><br><span class="line">	&#123;</span><br><span class="line">		Quote::<span class="keyword">operator</span>=(std::<span class="built_in">move</span>(bq));</span><br><span class="line">		min_qty = std::<span class="built_in">move</span>(bq.min_qty);</span><br><span class="line">		discount = std::<span class="built_in">move</span>(bq.discount);</span><br><span class="line">		cout &lt;&lt; <span class="string">&quot;Bulk_quote operator move = constructor&quot;</span> &lt;&lt; endl;</span><br><span class="line">		<span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="type">double</span> <span class="title">net_price</span><span class="params">(<span class="type">size_t</span>)</span> <span class="type">const</span> <span class="keyword">override</span></span>;</span><br><span class="line">	<span class="function"><span class="type">void</span> <span class="title">debug</span><span class="params">()</span> <span class="keyword">override</span></span>;</span><br><span class="line">	~<span class="built_in">Bulk_quote</span>() &#123; cout &lt;&lt; <span class="string">&quot;Bulk_quote destrucotr&quot;</span> &lt;&lt; endl; &#125;</span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">	<span class="type">size_t</span> min_qty = <span class="number">0</span>;</span><br><span class="line">	<span class="type">double</span> discount = <span class="number">0.0</span>;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">double</span> <span class="title">Bulk_quote::net_price</span><span class="params">(<span class="type">size_t</span> sz)</span> <span class="type">const</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">if</span> (sz &gt;= min_qty)</span><br><span class="line">		<span class="keyword">return</span> sz * (<span class="number">1</span> - discount) * price;</span><br><span class="line">	<span class="keyword">else</span></span><br><span class="line">		<span class="keyword">return</span> sz * price;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">Bulk_quote::debug</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	Quote::<span class="built_in">debug</span>();</span><br><span class="line">	cout &lt;&lt; <span class="string">&quot; min_qty: &quot;</span> &lt;&lt; min_qty &lt;&lt; <span class="string">&quot; discount: &quot;</span> &lt;&lt; discount &lt;&lt; endl;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="function">Quote <span class="title">q</span><span class="params">(<span class="string">&quot;199-9-X&quot;</span>, <span class="number">10.0</span>)</span></span>;</span><br><span class="line">	<span class="function">Bulk_quote <span class="title">bq</span><span class="params">(<span class="string">&quot;199-5-X&quot;</span>, <span class="number">15.0</span>, <span class="number">20</span>, <span class="number">0.2</span>)</span></span>;</span><br><span class="line"></span><br><span class="line">	<span class="function">Quote <span class="title">q1</span><span class="params">(q)</span></span>;</span><br><span class="line">	<span class="function">Bulk_quote <span class="title">bq2</span><span class="params">(bq)</span></span>;</span><br><span class="line"></span><br><span class="line">	q1 = q;</span><br><span class="line">	bq2 = bq;</span><br><span class="line"></span><br><span class="line">	q1 = std::<span class="built_in">move</span>(q);</span><br><span class="line">	bq2 = std::<span class="built_in">move</span>(bq);</span><br><span class="line"></span><br><span class="line">	<span class="built_in">system</span>(<span class="string">&quot;pause&quot;</span>);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="Exercises-Section-15-7-4"><a href="#Exercises-Section-15-7-4" class="headerlink" title="Exercises Section 15.7.4"></a>Exercises Section 15.7.4</h1><h2 id="Ex15-27"><a href="#Ex15-27" class="headerlink" title="Ex15.27"></a>Ex15.27</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Bulk_quote</span> : <span class="keyword">public</span> Quote</span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">	<span class="built_in">Bulk_quote</span>() = <span class="keyword">default</span>;</span><br><span class="line">	<span class="keyword">using</span> Quote::Quote;</span><br><span class="line">	<span class="built_in">Bulk_quote</span>(Bulk_quote &amp;bq): <span class="built_in">Quote</span>(bq), <span class="built_in">min_qty</span>(bq.min_qty), <span class="built_in">discount</span>(bq.discount) &#123; &#125;</span><br><span class="line">	<span class="built_in">Bulk_quote</span>(Bulk_quote &amp;&amp;bq): <span class="built_in">Quote</span>(std::<span class="built_in">move</span>(bq)), <span class="built_in">min_qty</span>(std::<span class="built_in">move</span>(bq.min_qty)), <span class="built_in">discount</span>(std::<span class="built_in">move</span>(bq.discount)) &#123; &#125;</span><br><span class="line">	Bulk_quote &amp; <span class="keyword">operator</span>=(<span class="type">const</span> Bulk_quote &amp;bq)</span><br><span class="line">	&#123;</span><br><span class="line">		Quote::<span class="keyword">operator</span>=(bq);</span><br><span class="line">		min_qty = bq.min_qty;</span><br><span class="line">		discount = bq.discount;</span><br><span class="line">		<span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	Bulk_quote &amp; <span class="keyword">operator</span>=(<span class="type">const</span> Bulk_quote &amp;&amp;bq)</span><br><span class="line">	&#123;</span><br><span class="line">		Quote::<span class="keyword">operator</span>=(std::<span class="built_in">move</span>(bq));</span><br><span class="line">		min_qty = std::<span class="built_in">move</span>(bq.min_qty);</span><br><span class="line">		discount = std::<span class="built_in">move</span>(bq.discount);</span><br><span class="line">		<span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="type">double</span> <span class="title">net_price</span><span class="params">(<span class="type">size_t</span>)</span> <span class="type">const</span> <span class="keyword">override</span></span>;</span><br><span class="line">	<span class="function"><span class="type">void</span> <span class="title">debug</span><span class="params">()</span> <span class="keyword">override</span></span>;</span><br><span class="line">	~<span class="built_in">Bulk_quote</span>();</span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">	<span class="type">size_t</span> min_qty = <span class="number">0</span>;</span><br><span class="line">	<span class="type">double</span> discount = <span class="number">0.0</span>;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h1 id="Exercises-Section-15-8"><a href="#Exercises-Section-15-8" class="headerlink" title="Exercises Section 15.8"></a>Exercises Section 15.8</h1><h2 id="Ex15-28"><a href="#Ex15-28" class="headerlink" title="Ex15.28"></a>Ex15.28</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;vector&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Quote</span></span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">	<span class="built_in">Quote</span>() = <span class="keyword">default</span>;</span><br><span class="line">	<span class="built_in">Quote</span>(<span class="type">const</span> string &amp;book, <span class="type">double</span> sales_price): <span class="built_in">bookNo</span>(book), <span class="built_in">price</span>(sales_price) &#123; &#125;</span><br><span class="line">	<span class="built_in">Quote</span>(Quote &amp;q): <span class="built_in">bookNo</span>(q.bookNo), <span class="built_in">price</span>(q.price) &#123; &#125;</span><br><span class="line">	<span class="built_in">Quote</span>(Quote &amp;&amp;q):<span class="built_in">bookNo</span>(std::<span class="built_in">move</span>(q.bookNo)), <span class="built_in">price</span>(std::<span class="built_in">move</span>(q.price)) &#123; &#125;</span><br><span class="line">	Quote &amp; <span class="keyword">operator</span>=(<span class="type">const</span> Quote &amp;q)</span><br><span class="line">	&#123;</span><br><span class="line">		bookNo = q.bookNo;</span><br><span class="line">		price = q.price;</span><br><span class="line">		<span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	Quote &amp; <span class="keyword">operator</span>=(Quote &amp;&amp;q)</span><br><span class="line">	&#123;</span><br><span class="line">		bookNo = std::<span class="built_in">move</span>(q.bookNo);</span><br><span class="line">		price = std::<span class="built_in">move</span>(q.price);</span><br><span class="line">		<span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function">string <span class="title">isbn</span><span class="params">()</span> <span class="type">const</span> </span>&#123; <span class="keyword">return</span> bookNo; &#125;</span><br><span class="line">	<span class="function"><span class="keyword">virtual</span> <span class="type">double</span> <span class="title">net_price</span><span class="params">(<span class="type">size_t</span> n)</span> <span class="type">const</span> </span></span><br><span class="line"><span class="function">	</span>&#123;</span><br><span class="line">		<span class="keyword">return</span> n * price;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">virtual</span> <span class="type">void</span> <span class="title">debug</span><span class="params">()</span> </span>&#123; &#125;</span><br><span class="line">	<span class="keyword">virtual</span> ~<span class="built_in">Quote</span>() &#123; &#125;</span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">	string bookNo;</span><br><span class="line"><span class="keyword">protected</span>:</span><br><span class="line">	<span class="type">double</span> price = <span class="number">0.0</span>;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Bulk_quote</span> : <span class="keyword">public</span> Quote</span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">	<span class="built_in">Bulk_quote</span>() = <span class="keyword">default</span>;</span><br><span class="line">	<span class="built_in">Bulk_quote</span>(<span class="type">const</span> string &amp;book, <span class="type">double</span> p, <span class="type">size_t</span> qty, <span class="type">double</span> disc):</span><br><span class="line">				<span class="built_in">Quote</span>(book, p), <span class="built_in">min_qty</span>(qty), <span class="built_in">discount</span>(disc) &#123; &#125;</span><br><span class="line">	<span class="built_in">Bulk_quote</span>(Bulk_quote &amp;bq): <span class="built_in">Quote</span>(bq), <span class="built_in">min_qty</span>(bq.min_qty), <span class="built_in">discount</span>(bq.discount) &#123; &#125;</span><br><span class="line">	<span class="built_in">Bulk_quote</span>(Bulk_quote &amp;&amp;bq): <span class="built_in">Quote</span>(std::<span class="built_in">move</span>(bq)), <span class="built_in">min_qty</span>(std::<span class="built_in">move</span>(bq.min_qty)), <span class="built_in">discount</span>(std::<span class="built_in">move</span>(bq.discount)) &#123; &#125;</span><br><span class="line">	Bulk_quote &amp; <span class="keyword">operator</span>=(<span class="type">const</span> Bulk_quote &amp;bq)</span><br><span class="line">	&#123;</span><br><span class="line">		Quote::<span class="keyword">operator</span>=(bq);</span><br><span class="line">		min_qty = bq.min_qty;</span><br><span class="line">		discount = bq.discount;</span><br><span class="line">		<span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	Bulk_quote &amp; <span class="keyword">operator</span>=(<span class="type">const</span> Bulk_quote &amp;&amp;bq)</span><br><span class="line">	&#123;</span><br><span class="line">		Quote::<span class="keyword">operator</span>=(std::<span class="built_in">move</span>(bq));</span><br><span class="line">		min_qty = std::<span class="built_in">move</span>(bq.min_qty);</span><br><span class="line">		discount = std::<span class="built_in">move</span>(bq.discount);</span><br><span class="line">		<span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="type">double</span> <span class="title">net_price</span><span class="params">(<span class="type">size_t</span>)</span> <span class="type">const</span> <span class="keyword">override</span></span>;</span><br><span class="line">	<span class="function"><span class="type">void</span> <span class="title">debug</span><span class="params">()</span> <span class="keyword">override</span></span>;</span><br><span class="line">	~<span class="built_in">Bulk_quote</span>() &#123; &#125;</span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">	<span class="type">size_t</span> min_qty = <span class="number">0</span>;</span><br><span class="line">	<span class="type">double</span> discount = <span class="number">0.0</span>;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">double</span> <span class="title">Bulk_quote::net_price</span><span class="params">(<span class="type">size_t</span> sz)</span> <span class="type">const</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">if</span> (sz &gt;= min_qty)</span><br><span class="line">		<span class="keyword">return</span> sz * (<span class="number">1</span> - discount) * price;</span><br><span class="line">	<span class="keyword">else</span></span><br><span class="line">		<span class="keyword">return</span> sz * price;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">Bulk_quote::debug</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	Quote::<span class="built_in">debug</span>();</span><br><span class="line">	cout &lt;&lt; <span class="string">&quot; min_qty: &quot;</span> &lt;&lt; min_qty &lt;&lt; <span class="string">&quot; discount: &quot;</span> &lt;&lt; discount &lt;&lt; endl;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	vector&lt;Quote&gt; basket;</span><br><span class="line">	basket.<span class="built_in">push_back</span>(<span class="built_in">Bulk_quote</span>(<span class="string">&quot;199-9-X&quot;</span>, <span class="number">50.0</span>, <span class="number">10</span>, <span class="number">0.2</span>));</span><br><span class="line">	<span class="type">double</span> sum = <span class="number">0.0</span>;</span><br><span class="line">	<span class="keyword">for</span> (<span class="keyword">auto</span> &amp;v : basket)</span><br><span class="line">		sum += v.<span class="built_in">net_price</span>(<span class="number">20</span>);</span><br><span class="line">	cout &lt;&lt; sum &lt;&lt; endl;</span><br><span class="line">	<span class="built_in">system</span>(<span class="string">&quot;pause&quot;</span>);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="Ex15-29"><a href="#Ex15-29" class="headerlink" title="Ex15.29"></a>Ex15.29</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;memory&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Quote</span></span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">	<span class="built_in">Quote</span>() = <span class="keyword">default</span>;</span><br><span class="line">	<span class="built_in">Quote</span>(<span class="type">const</span> string &amp;book, <span class="type">double</span> sales_price): <span class="built_in">bookNo</span>(book), <span class="built_in">price</span>(sales_price) &#123; &#125;</span><br><span class="line">	<span class="built_in">Quote</span>(Quote &amp;q): <span class="built_in">bookNo</span>(q.bookNo), <span class="built_in">price</span>(q.price) &#123; &#125;</span><br><span class="line">	<span class="built_in">Quote</span>(Quote &amp;&amp;q):<span class="built_in">bookNo</span>(std::<span class="built_in">move</span>(q.bookNo)), <span class="built_in">price</span>(std::<span class="built_in">move</span>(q.price)) &#123; &#125;</span><br><span class="line">	Quote &amp; <span class="keyword">operator</span>=(<span class="type">const</span> Quote &amp;q)</span><br><span class="line">	&#123;</span><br><span class="line">		bookNo = q.bookNo;</span><br><span class="line">		price = q.price;</span><br><span class="line">		<span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	Quote &amp; <span class="keyword">operator</span>=(Quote &amp;&amp;q)</span><br><span class="line">	&#123;</span><br><span class="line">		bookNo = std::<span class="built_in">move</span>(q.bookNo);</span><br><span class="line">		price = std::<span class="built_in">move</span>(q.price);</span><br><span class="line">		<span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function">string <span class="title">isbn</span><span class="params">()</span> <span class="type">const</span> </span>&#123; <span class="keyword">return</span> bookNo; &#125;</span><br><span class="line">	<span class="function"><span class="keyword">virtual</span> <span class="type">double</span> <span class="title">net_price</span><span class="params">(<span class="type">size_t</span> n)</span> <span class="type">const</span> </span></span><br><span class="line"><span class="function">	</span>&#123;</span><br><span class="line">		<span class="keyword">return</span> n * price;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">virtual</span> <span class="type">void</span> <span class="title">debug</span><span class="params">()</span> </span>&#123; &#125;</span><br><span class="line">	<span class="keyword">virtual</span> ~<span class="built_in">Quote</span>() &#123; &#125;</span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">	string bookNo;</span><br><span class="line"><span class="keyword">protected</span>:</span><br><span class="line">	<span class="type">double</span> price = <span class="number">0.0</span>;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Bulk_quote</span> : <span class="keyword">public</span> Quote</span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">	<span class="built_in">Bulk_quote</span>() = <span class="keyword">default</span>;</span><br><span class="line">	<span class="built_in">Bulk_quote</span>(<span class="type">const</span> string &amp;book, <span class="type">double</span> p, <span class="type">size_t</span> qty, <span class="type">double</span> disc):</span><br><span class="line">				<span class="built_in">Quote</span>(book, p), <span class="built_in">min_qty</span>(qty), <span class="built_in">discount</span>(disc) &#123; &#125;</span><br><span class="line">	<span class="built_in">Bulk_quote</span>(Bulk_quote &amp;bq): <span class="built_in">Quote</span>(bq), <span class="built_in">min_qty</span>(bq.min_qty), <span class="built_in">discount</span>(bq.discount) &#123; &#125;</span><br><span class="line">	<span class="built_in">Bulk_quote</span>(Bulk_quote &amp;&amp;bq): <span class="built_in">Quote</span>(std::<span class="built_in">move</span>(bq)), <span class="built_in">min_qty</span>(std::<span class="built_in">move</span>(bq.min_qty)), <span class="built_in">discount</span>(std::<span class="built_in">move</span>(bq.discount)) &#123; &#125;</span><br><span class="line">	Bulk_quote &amp; <span class="keyword">operator</span>=(<span class="type">const</span> Bulk_quote &amp;bq)</span><br><span class="line">	&#123;</span><br><span class="line">		Quote::<span class="keyword">operator</span>=(bq);</span><br><span class="line">		min_qty = bq.min_qty;</span><br><span class="line">		discount = bq.discount;</span><br><span class="line">		<span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	Bulk_quote &amp; <span class="keyword">operator</span>=(<span class="type">const</span> Bulk_quote &amp;&amp;bq)</span><br><span class="line">	&#123;</span><br><span class="line">		Quote::<span class="keyword">operator</span>=(std::<span class="built_in">move</span>(bq));</span><br><span class="line">		min_qty = std::<span class="built_in">move</span>(bq.min_qty);</span><br><span class="line">		discount = std::<span class="built_in">move</span>(bq.discount);</span><br><span class="line">		<span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="type">double</span> <span class="title">net_price</span><span class="params">(<span class="type">size_t</span>)</span> <span class="type">const</span> <span class="keyword">override</span></span>;</span><br><span class="line">	<span class="function"><span class="type">void</span> <span class="title">debug</span><span class="params">()</span> <span class="keyword">override</span></span>;</span><br><span class="line">	~<span class="built_in">Bulk_quote</span>() &#123; &#125;</span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">	<span class="type">size_t</span> min_qty = <span class="number">0</span>;</span><br><span class="line">	<span class="type">double</span> discount = <span class="number">0.0</span>;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">double</span> <span class="title">Bulk_quote::net_price</span><span class="params">(<span class="type">size_t</span> sz)</span> <span class="type">const</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">if</span> (sz &gt;= min_qty)</span><br><span class="line">		<span class="keyword">return</span> sz * (<span class="number">1</span> - discount) * price;</span><br><span class="line">	<span class="keyword">else</span></span><br><span class="line">		<span class="keyword">return</span> sz * price;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">Bulk_quote::debug</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	Quote::<span class="built_in">debug</span>();</span><br><span class="line">	cout &lt;&lt; <span class="string">&quot; min_qty: &quot;</span> &lt;&lt; min_qty &lt;&lt; <span class="string">&quot; discount: &quot;</span> &lt;&lt; discount &lt;&lt; endl;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	vector&lt;shared_ptr&lt;Quote&gt;&gt; basket;</span><br><span class="line">	basket.<span class="built_in">push_back</span>(<span class="built_in">make_shared</span>&lt;Bulk_quote&gt;(<span class="string">&quot;199-9-X&quot;</span>, <span class="number">50.0</span>, <span class="number">10</span>, <span class="number">0.2</span>));</span><br><span class="line">	<span class="type">double</span> sum = <span class="number">0.0</span>;</span><br><span class="line">	<span class="keyword">for</span> (<span class="keyword">auto</span> &amp;v : basket)</span><br><span class="line">		sum += v-&gt;<span class="built_in">net_price</span>(<span class="number">20</span>);</span><br><span class="line">	cout &lt;&lt; sum &lt;&lt; endl;</span><br><span class="line">	<span class="built_in">system</span>(<span class="string">&quot;pause&quot;</span>);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>如果为 <code>vector&lt;Quote&gt;</code> 则使用 Quote 类中的 net_price 函数计算，不管添加的类型是否为 Bulk_quote；<br>如果为 <code>vector&lt;shared_ptr&lt;Quote&gt;&gt;</code> 则用 Bulk_quote 的 net_price 函数。</p>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="https://astralcon.gitee.io/blog">astralcon</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://astralcon.gitee.io/blog/2022/05/14/Chapter-15-Object-Oriented-Programming/">https://astralcon.gitee.io/blog/2022/05/14/Chapter-15-Object-Oriented-Programming/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://astralcon.gitee.io/blog" target="_blank">A君的秘密基地</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/blog/tags/C-Primer/">C++ Primer</a></div><div class="post_share"><div class="social-share" data-image="" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/gh/overtrue/share.js@master/dist/js/social-share.min.js" defer></script></div></div><div class="post-reward"><div class="reward-button"><i class="fas fa-qrcode"></i> 打赏</div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="/blog/img/wechat.jpg" target="_blank"><img class="post-qr-code-img" src="/blog/img/wechat.jpg" alt="微信"/></a><div class="post-qr-code-desc">微信</div></li><li class="reward-item"><a href="/blog/img/alipay.jpg" target="_blank"><img class="post-qr-code-img" src="/blog/img/alipay.jpg" alt="支付宝"/></a><div class="post-qr-code-desc">支付宝</div></li></ul></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/blog/2022/05/14/Chapter-16-Templates-And-Generic-Programming/"><img class="prev-cover" src="" onerror="onerror=null;src='/blog/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">Chapter 16: Templates And Generic Programming</div></div></a></div><div class="next-post pull-right"><a href="/blog/2022/05/14/Chapter-14-Overloaded-Operations-And-Conversions/"><img class="next-cover" src="" onerror="onerror=null;src='/blog/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">Chapter 14: Overloaded Operations And Conversions</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/blog/2022/05/14/Chapter-1-Getting-Started/" title="Chapter 1: Getting Started"><img class="cover" src="" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-05-14</div><div class="title">Chapter 1: Getting Started</div></div></a></div><div><a href="/blog/2022/05/14/Chapter-10-Generic-Algorithms/" title="Chapter 10: Generic Algorithms"><img class="cover" src="" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-05-14</div><div class="title">Chapter 10: Generic Algorithms</div></div></a></div><div><a href="/blog/2022/05/14/Chapter-11-Associative-Containers/" title="Chapter 11: Associative Containers"><img class="cover" src="" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-05-14</div><div class="title">Chapter 11: Associative Containers</div></div></a></div><div><a href="/blog/2022/05/14/Chapter-12-Dynamic-Memory/" title="Chapter 12: Dynamic Memory"><img class="cover" src="" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-05-14</div><div class="title">Chapter 12: Dynamic Memory</div></div></a></div><div><a href="/blog/2022/05/14/Chapter-13-Copy-Control/" title="Chapter 13: Copy Control"><img class="cover" src="" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-05-14</div><div class="title">Chapter 13: Copy Control</div></div></a></div><div><a href="/blog/2022/05/14/Chapter-14-Overloaded-Operations-And-Conversions/" title="Chapter 14: Overloaded Operations And Conversions"><img class="cover" src="" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-05-14</div><div class="title">Chapter 14: Overloaded Operations And Conversions</div></div></a></div></div></div><hr/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i><span> 评论</span></div><div id="comment-switch"><span class="first-comment">Valine</span><span class="switch-btn"></span><span class="second-comment">Disqus</span></div></div><div class="comment-wrap"><div><div class="vcomment" id="vcomment"></div></div><div><div id="disqus_thread"></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/blog/img/avatar.jpg" onerror="this.onerror=null;this.src='/blog/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">astralcon</div><div class="author-info__description">slow is fast.</div></div><div class="card-info-data site-data is-center"><a href="/blog/archives/"><div class="headline">文章</div><div class="length-num">27</div></a><a href="/blog/tags/"><div class="headline">标签</div><div class="length-num">4</div></a><a href="/blog/categories/"><div class="headline">分类</div><div class="length-num">3</div></a></div><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/astralcon" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="mailto:a842181471@qq.com.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">朝九晚五，健康生活！</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex15-1"><span class="toc-number">1.</span> <span class="toc-text">Ex15.1</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex15-2"><span class="toc-number">2.</span> <span class="toc-text">Ex15.2</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex15-3"><span class="toc-number">3.</span> <span class="toc-text">Ex15.3</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Exercises-Section-15-2-2"><span class="toc-number"></span> <span class="toc-text">Exercises Section 15.2.2</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex15-4"><span class="toc-number">1.</span> <span class="toc-text">Ex15.4</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex15-5"><span class="toc-number">2.</span> <span class="toc-text">Ex15.5</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex15-6"><span class="toc-number">3.</span> <span class="toc-text">Ex15.6</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex15-7"><span class="toc-number">4.</span> <span class="toc-text">Ex15.7</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Exercises-Section-15-2-3"><span class="toc-number"></span> <span class="toc-text">Exercises Section 15.2.3</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex15-8"><span class="toc-number">1.</span> <span class="toc-text">Ex15.8</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex15-9"><span class="toc-number">2.</span> <span class="toc-text">Ex15.9</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex15-10"><span class="toc-number">3.</span> <span class="toc-text">Ex15.10</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Exercises-Section-15-3"><span class="toc-number"></span> <span class="toc-text">Exercises Section 15.3</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex15-11"><span class="toc-number">1.</span> <span class="toc-text">Ex15.11</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex15-12"><span class="toc-number">2.</span> <span class="toc-text">Ex15.12</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex15-13"><span class="toc-number">3.</span> <span class="toc-text">Ex15.13</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex15-14"><span class="toc-number">4.</span> <span class="toc-text">Ex15.14</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Exercises-Section-15-4"><span class="toc-number"></span> <span class="toc-text">Exercises Section 15.4</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex15-15"><span class="toc-number">1.</span> <span class="toc-text">Ex15.15</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex15-16"><span class="toc-number">2.</span> <span class="toc-text">Ex15.16</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex15-17"><span class="toc-number">3.</span> <span class="toc-text">Ex15.17</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Exercises-Section-15-5"><span class="toc-number"></span> <span class="toc-text">Exercises Section 15.5</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex15-18"><span class="toc-number">1.</span> <span class="toc-text">Ex15.18</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex15-19"><span class="toc-number">2.</span> <span class="toc-text">Ex15.19</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex15-20"><span class="toc-number">3.</span> <span class="toc-text">Ex15.20</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex-15-21"><span class="toc-number">4.</span> <span class="toc-text">Ex 15.21</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex15-22"><span class="toc-number">5.</span> <span class="toc-text">Ex15.22</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Exercises-Section-15-6"><span class="toc-number"></span> <span class="toc-text">Exercises Section 15.6</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex15-23"><span class="toc-number">1.</span> <span class="toc-text">Ex15.23</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Exercises-Section-15-7-1"><span class="toc-number"></span> <span class="toc-text">Exercises Section 15.7.1</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex15-24"><span class="toc-number">1.</span> <span class="toc-text">Ex15.24</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Exercises-Section-15-7-2"><span class="toc-number"></span> <span class="toc-text">Exercises Section 15.7.2</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex15-25"><span class="toc-number">1.</span> <span class="toc-text">Ex15.25</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Exercises-Section-15-7-3"><span class="toc-number"></span> <span class="toc-text">Exercises Section 15.7.3</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex15-26"><span class="toc-number">1.</span> <span class="toc-text">Ex15.26</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Exercises-Section-15-7-4"><span class="toc-number"></span> <span class="toc-text">Exercises Section 15.7.4</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex15-27"><span class="toc-number">1.</span> <span class="toc-text">Ex15.27</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Exercises-Section-15-8"><span class="toc-number"></span> <span class="toc-text">Exercises Section 15.8</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex15-28"><span class="toc-number">1.</span> <span class="toc-text">Ex15.28</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex15-29"><span class="toc-number">2.</span> <span class="toc-text">Ex15.29</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/blog/2022/05/17/%E3%80%8A%E7%AE%97%E6%B3%95%E8%AE%BE%E8%AE%A1%E6%89%8B%E5%86%8C%E3%80%8B%E9%9D%A2%E8%AF%95%E9%A2%98%EF%BC%9AChapter-1/" title="《算法设计手册》面试题：Chapter 1">《算法设计手册》面试题：Chapter 1</a><time datetime="2022-05-17T07:35:05.000Z" title="发表于 2022-05-17 15:35:05">2022-05-17</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/blog/2022/05/14/Wireshark-Lab-DHCP-v7-0/" title="Wireshark Lab: DHCP v7.0">Wireshark Lab: DHCP v7.0</a><time datetime="2022-05-14T07:19:53.000Z" title="发表于 2022-05-14 15:19:53">2022-05-14</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/blog/2022/05/14/Wireshark-Lab-Ethernet-and-ARP-v7-0/" title="Wireshark Lab: Ethernet and ARP v7.0">Wireshark Lab: Ethernet and ARP v7.0</a><time datetime="2022-05-14T07:18:10.000Z" title="发表于 2022-05-14 15:18:10">2022-05-14</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/blog/2022/05/14/Wireshark-Lab-ICMP-v7-0/" title="Wireshark Lab: ICMP v7.0">Wireshark Lab: ICMP v7.0</a><time datetime="2022-05-14T07:16:41.000Z" title="发表于 2022-05-14 15:16:41">2022-05-14</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/blog/2022/05/14/Wireshark-Lab-NAT-v7-0/" title="Wireshark Lab: NAT v7.0">Wireshark Lab: NAT v7.0</a><time datetime="2022-05-14T07:15:13.000Z" title="发表于 2022-05-14 15:15:13">2022-05-14</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2022 By astralcon</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="algolia-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="search-wrap"><div id="algolia-search-input"></div><hr/><div id="algolia-search-results"><div id="algolia-hits"></div><div id="algolia-pagination"></div><div id="algolia-info"><div class="algolia-stats"></div><div class="algolia-poweredBy"></div></div></div></div></div><div id="search-mask"></div></div><div><script src="/blog/js/utils.js"></script><script src="/blog/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.js"></script><script src="https://cdn.jsdelivr.net/npm/algoliasearch@4/dist/algoliasearch-lite.umd.js"></script><script src="https://cdn.jsdelivr.net/npm/instantsearch.js@4/dist/instantsearch.production.min.js"></script><script src="/blog/js/search/algolia.js"></script><script>var preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',preloader.endLoading())</script><div class="js-pjax"><script>function loadValine () {
  function initValine () {
    const valine = new Valine(Object.assign({
      el: '#vcomment',
      appId: 'Uh3CYGE2t1Eaz4KoTkyVQv1N-gzGzoHsz',
      appKey: '1Gdg4rGlmT4Dcfv7oNiCH1q3',
      avatar: 'monsterid',
      serverURLs: '',
      emojiMaps: "",
      path: window.location.pathname,
      visitor: false
    }, null))
  }

  if (typeof Valine === 'function') initValine() 
  else getScript('https://cdn.jsdelivr.net/npm/valine/dist/Valine.min.js').then(initValine)
}

if ('Valine' === 'Valine' || !false) {
  if (false) btf.loadComment(document.getElementById('vcomment'),loadValine)
  else setTimeout(loadValine, 0)
} else {
  function loadOtherComment () {
    loadValine()
  }
}</script><script>function loadDisqus () {
  var disqus_config = function () {
    this.page.url = 'https://astralcon.gitee.io/blog/2022/05/14/Chapter-15-Object-Oriented-Programming/'
    this.page.identifier = '2022/05/14/Chapter-15-Object-Oriented-Programming/'
    this.page.title = 'Chapter 15: Object-Oriented Programming'
  };

  window.disqusReset = () => {
    DISQUS.reset({
      reload: true,
      config: disqus_config
    })
  }

  if (window.DISQUS) disqusReset()
  else {
    (function() { 
      var d = document, s = d.createElement('script');
      s.src = 'https://.disqus.com/embed.js';
      s.setAttribute('data-timestamp', +new Date());
      (d.head || d.body).appendChild(s);
    })();
  }
}

if ('Valine' === 'Disqus' || !false) {
  if (false) btf.loadComment(document.getElementById('disqus_thread'), loadDisqus)
  else loadDisqus()
} else {
  function loadOtherComment () {
    loadDisqus()
  }
}
</script></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><script src="/blog/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"log":false,"pluginJsPath":"lib/","pluginModelPath":"assets/","pluginRootPath":"live2dw/","tagMode":false});</script></body></html>